package ui_logic;

import connect.Broadcast;
import connect.Client;
import ui_logic.logic.Receive;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;


public class MatchUI extends Thread {
    private JLabel title;
    private JButton sendM;
    private JPanel panel1;
    private JTextField word;


    private JPanel playersPanel;
    private JButton update;
    private final JFrame frame;
    private final Client client;

    private Broadcast broadcast;

    private ArrayList<String> receiveString = new ArrayList<>();


    public MatchUI() {
        playersPanel.setLayout(new GridLayout(0, 1));
        title.setText("排队列表");
        word.setText("来下盘棋");
        sendM.setText("发送匹配请求");

        frame = new JFrame("进行局域网连接");
        //设置窗口大小
        frame.setSize(350, 450);
//        frame.setLayout(new FlowLayout());
        frame.setContentPane(panel1);
        //设置按下右上角X号后关闭
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        //窗口居中
        frame.setLocationRelativeTo(null);
        //窗口可见
        frame.setVisible(true);
        try {
            client = new Client();
            broadcast = new Broadcast();
            broadcast.start();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void startMatching () {
        sendM.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                word.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyTyped(KeyEvent e) {
                        super.keyTyped(e);
                        String checkIP = word.getText();
                        if (checkIP.length() >= 15) e.consume();
                    }
                });
                Broadcast.callSendBroadMessage(word.getText());
            }
        });
        update.addActionListener(e -> {
            Broadcast.resetBroadFinish();
            try {
                receiveString = broadcast.getBroadcastMsg();
                playersPanel.removeAll();
                playersPanel.revalidate();
                for (String name : receiveString) {
                    // 通过点击按钮转移到单播
                    JButton newButton = new JButton(name);
                    newButton.addActionListener(e1 -> {
                        Broadcast.setBroadFinish();
                        Broadcast.callSendBroadMessage("有人找我了");
                        String info = JOptionPane.showInputDialog(null, "内容", "输入想说的话");        //输入对话框
                        String ip = name.substring(0, name.indexOf(':'));
                        Client.callSendMessage(ip, info);
                    });
                    playersPanel.add(newButton);
                    playersPanel.revalidate();
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        panel1.revalidate();
        frame.setContentPane(panel1);
        //窗口可见
        frame.setVisible(true);
    }


    @Override
    public void run() {
        Receive r = new Receive();
        while (r.receiveDone != 1) {
            Client.callReceiveMessage();
            r.receiveBroad(broadcast, client, panel1);
        }
        System.out.println("即将进入游戏");
    }

    /**
     * 该方法用于测试UI
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MatchUI t = new MatchUI();
            t.startMatching();
            t.start();
        });
    }
}

